package com.rzico.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@Configuration
public class TomcatConfig {

    @org.springframework.beans.factory.annotation.Value("${server.port}")
    private Integer port;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize(DataSize.parse("80MB")); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.parse("102400KB"));
        return factory.createMultipartConfig();
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                if (port == 8080) {
                    return;
                }
                if (port == 9090) {
                    return;
                }
//                if (factory instanceof TomcatServletWebServerFactory) {
//                    TomcatServletWebServerFactory webServerFactory = (TomcatServletWebServerFactory)factory;
//                    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
//                    // 设置http访问的端口号，不能与https端口重复，否则会报端口被占用的错误
//                    connector.setPort(8080);
//                    webServerFactory.addAdditionalTomcatConnectors(connector);
//
//                    Connector connector1 = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
//                    // 设置http访问的端口号，不能与https端口重复，否则会报端口被占用的错误
//                    connector1.setPort(19080);
//                    webServerFactory.addAdditionalTomcatConnectors(connector1);
//                }
            }
        };
    }

}